/* This file is part of QGoogleTranslate
 *
 * Copyright (C) 2009 Durachenko Alexei V. <lexnewton86@gmail.com>
 */

#include "mainwindow.h"
#include "ceditorwidget.h"
#include "cconfigdialog.h"
#include "config.h"

CMainWindow::CMainWindow( QWidget * parent, Qt::WFlags f)
	: QMainWindow(parent, f)
{
    // init UI
	setupUi( this );
	setWindowTitle( G_FULL_PROGRAM_NAME );
    tabWidget->clear();

    // load settings
    m_Settings = 0;
    loadSettings();

    // create button for append tab
    QToolButton* newToolButton = new QToolButton( this );
    newToolButton->setDefaultAction( action_New );
    newToolButton->setAutoRaise( true );
    tabWidget->setCornerWidget( newToolButton, Qt::TopLeftCorner );

    // create button for close tab
    QToolButton* closeToolButton = new QToolButton( this );
    closeToolButton->setDefaultAction( action_Close );
    closeToolButton->setAutoRaise( true );
    tabWidget->setCornerWidget( closeToolButton, Qt::TopRightCorner );

    // create one empty tab
    on_action_New_triggered();

    // initialization tray icon
    QMenu* systemTrayMenu = new QMenu( this );
    systemTrayMenu->addAction( action_TranslateBuffer );
    systemTrayMenu->addAction( action_ShowHide );
    systemTrayMenu->addSeparator();
    systemTrayMenu->addAction( action_Quit );

    m_SystemTrayIcon = new QSystemTrayIcon( QIcon(":/icons/icons/wicon.png"), this );
    m_SystemTrayIcon->setVisible( true );
    m_SystemTrayIcon->setContextMenu( systemTrayMenu );
    m_SystemTrayIcon->setToolTip( G_FULL_PROGRAM_NAME );

    connect( m_SystemTrayIcon, SIGNAL( activated( QSystemTrayIcon::ActivationReason ) ), SLOT( slot_m_SystemTrayIcon_activated( QSystemTrayIcon::ActivationReason ) ) );
}

CMainWindow::~CMainWindow()
{
    if( m_Settings )
    {
        saveSettings();
        delete m_Settings;
    }
}

void CMainWindow::saveSettings()
{
    m_Settings->setValue( "MainWindow/state", saveState() );
    m_Settings->setValue( "MainWindow/geometry", saveGeometry() );
}


void CMainWindow::loadSettings()
{
    if( m_Settings )
    {
        saveSettings();
        delete m_Settings;
    }
    m_Settings = new QSettings( QSettings::IniFormat, QSettings::UserScope, gProjectFamily, gProjectName );

    restoreState( m_Settings->value("MainWindow/state",saveState()).toByteArray () );
    restoreGeometry( m_Settings->value("MainWindow/geometry",saveGeometry()).toByteArray() );
}


void CMainWindow::updateToolButtonState()
{
    action_Close->setEnabled( false );
    if( tabWidget->count() > 1 )
    {
        action_Close->setEnabled( true );
    }
}

void CMainWindow::slot_m_SystemTrayIcon_activated( QSystemTrayIcon::ActivationReason reason )
{
    if( reason == QSystemTrayIcon::DoubleClick )
    {
        on_action_ShowHide_triggered();
    }
}


void CMainWindow::on_action_ShowHide_triggered()
{
	setVisible( ! isVisible() );
}


void CMainWindow::on_action_New_triggered()
{
    CEditorWidget* curWidget = new CEditorWidget( this );
	tabWidget->addTab( curWidget, QIcon(":/icons/icons/empty.png"), "" );
	tabWidget->setCurrentWidget( curWidget );
	curWidget->setTabWidget( tabWidget );
	curWidget->setSourceLanguageIndex( m_Settings->value("DefaultSourceLanguageIndex",0).toInt() );
	curWidget->setDestinationLanguageIndex( m_Settings->value("DefaultDestinationLanguageIndex",0).toInt() );
	updateToolButtonState();
}

void CMainWindow::on_action_TranslateBuffer_triggered()
{
	if( m_Settings->value("TranslateOfBufferInNewTab", false).toBool() == true )
	{
	    on_action_New_triggered();
	}

    CEditorWidget* curWidget = qobject_cast<CEditorWidget*>( tabWidget->currentWidget() );

    QClipboard *clipboard = QApplication::clipboard();
    QString text = clipboard->text();
    if( ! text.isEmpty() )
    {
        curWidget->setSourceText( text );
        curWidget->translate();
    }

    setVisible( true );
}

void CMainWindow::on_action_Close_triggered()
{
	if( tabWidget->count() > 1 && tabWidget->currentIndex() != -1 )
	{
	    tabWidget->removeTab( tabWidget->currentIndex() );
	    updateToolButtonState();
	}
}


void CMainWindow::on_action_Quit_triggered()
{
    saveSettings();
	qApp->quit();
}


void CMainWindow::on_action_Preferencec_triggered()
{
	CConfigDialog dialog( this );

    dialog.setSettingsObject( m_Settings );

	if( dialog.exec() == QDialog::Accepted )
	{
	}
}


void CMainWindow::on_action_About_QGoogleTranslate_triggered()
{
	QMessageBox::about( this, tr("About"),
        G_FULL_PROGRAM_NAME + "\n\n" +
        tr("Author: Durachenko Alexei V. <lexnewton86@gmail.com>") );
}


void CMainWindow::on_action_AboutQt_triggered()
{
	qApp->aboutQt();
}


void CMainWindow::closeEvent( QCloseEvent* event )
{
    on_action_ShowHide_triggered();
    event->ignore();
}
